
#include <qdatastream.h>
#include <qmetatype.h>

#include <QMouseEvent>

// header
#include "myglwidget.h"

// GMlib
#include <gmCamera.h>
#include <gmCameraIso.h>

#include <gmParametrics>
#include <gmVisualizers>

using namespace GMlib;


MyGLWidget::MyGLWidget(QWidget *parent) : GLWidget( parent ) {}

void MyGLWidget::initScene() {

  // Insert a light
  GMlib::Point3D<GLfloat> init_light_pos( 2.0, 4.0, 10 );
  insertLight( new GMlib::PointLight( GMlib::GMcolor::White, GMlib::GMcolor::White, GMlib::GMcolor::White, init_light_pos ), false );

  // Insert Sun
  insertSun();



  // Camera
  GMlib::Point3D<float> init_cam_pos(  0.0f, 0.0f, 0.0f );
  GMlib::Vector3D<float> init_cam_dir( 0.0f, 1.0f, 0.0f );
  GMlib::Vector3D<float> init_cam_up(  0.0f, 0.0f, 1.0f );



  GMlib::Camera *cam_project = new GMlib::Camera( init_cam_pos, init_cam_dir, init_cam_up );
  cam_project->setCuttingPlanes( 1.0f, 8000.0f );
  cam_project->rotateGlobal( GMlib::Angle(-45), GMlib::Vector3D<float>( 1.0f, 0.0f, 0.0f ) );
  cam_project->translate( GMlib::Vector3D<float>( 0.0f, -20.0f, 20.0f ) );

    /*
  // Front
  GMlib::CameraIso *iso_front = new GMlib::CameraIso( init_cam_pos + GMlib::Vector3D<float>( 0.0f, -50.0f, 0.0f ), init_cam_dir, init_cam_up );
  iso_front->setCuttingPlanes( 1.0f, 8000.0f );
  iso_front->zoom( 5.0 );

  // Side
  GMlib::CameraIso *iso_side = new GMlib::CameraIso( init_cam_pos + GMlib::Vector3D<float>( -50.0f, 0.0f, 0.0f ), GMlib::Vector3D<float>( 1.0f, 0.0f, 0.0f ), init_cam_up );
  iso_side->setCuttingPlanes( 1.0f, 8000.0f );
  iso_side->zoom( 5.0 );

  // Up
  GMlib::CameraIso *iso_top = new GMlib::CameraIso( init_cam_pos + GMlib::Vector3D<float>( 0.0f, 0.0f, 50.0f ), -init_cam_up, init_cam_dir );
  iso_top->setCuttingPlanes( 1.0f, 8000.0f );
  iso_top->zoom( 10.0 );
  */


  insertCamera( cam_project );
  /*
  insertCamera( iso_front );
  insertCamera( iso_side );
  insertCamera( iso_top );
*/

  int cam_project_idx = _cameras.index( cam_project );
 /*
  int iso_front_idx = _cameras.index( iso_front );
  int iso_side_idx = _cameras.index( iso_side );
  int iso_top_idx = _cameras.index( iso_top );
*/


  setViewSet( cam_project_idx );
  /*
  setViewSet( iso_top_idx, cam_project_idx, false );
  setViewSet( iso_front_idx, iso_top_idx, true );
  setViewSet( iso_side_idx, iso_front_idx, false );
*/


  // Create a Torus
  PTorus<float> *torus = new PTorus<float>();

  // Create a Color Contour Visualizer
  PSurfContoursVisualizer<float> *visu = new PSurfContoursVisualizer<float>();

  // Insert the visualizer
  torus->insertVisualizer( visu );

  // Turn off the default Visualizer
  torus->toggleDefaultVisualizer();

  //this->insert(torus);

  // Replot
  //torus->replot( 10, 10 );


}

void MyGLWidget::mouseDoubleClickEvent(QMouseEvent *e) {

  int x = e->x();
  int y = _h - e->y() - 1;

  if( e->buttons() == Qt::LeftButton && e->modifiers() == Qt::NoModifier ) {

    if( GMWindow::find( x, y, _active_cam ) ) {

      if( _view_set_stack.getSize() > 2 )
        _view_set_stack.pop();
      else
        _view_set_stack += GMlib::ViewSet( _cameras[_active_cam] );

      _view_set_stack.back().prepare(_w,_h);
    }
  }

  // Store coords
  _hid_prevX = x;
  _hid_prevY = y;

  // Update GL
  updateGL();
}
